/**
$Id: Caduceus.hpp 3096 2010-07-06 07:21:14Z Bear $

Copyright (c) 2010 Nuwa Information Co., Ltd, and individual contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  3. Neither the name of Nuwa Information nor the names of its contributors
     may be used to endorse or promote products derived from this software
     without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

$Author: Bear $
$Date: 2010-07-06 15:21:14 +0800 (星期二, 06 七月 2010) $
$Revision: 3096 $
*/

#ifndef _INCLUDED_CADUCEUS_
#define _INCLUDED_CADUCEUS_

#ifdef _PYTHON

#include <string>

#include <boost/cstdint.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/python.hpp>

#include <pion/pionconfig.hpp>

// Public types.
#include <pion/net/WebServer.hpp>

typedef boost::shared_ptr<pion::PionScheduler> PionSchedulerPtr;

typedef boost::shared_ptr<pion::PionSingleServiceScheduler> 
 PionSingleServiceSchedulerPtr;

typedef boost::shared_ptr<pion::PionOneToOneScheduler> 
 PionOneToOneSchedulerPtr;

struct PionSchedulerFactory
{
 int threads;

 PionSchedulerFactory(int _threads = 8) : threads(_threads)
 {
 }

 virtual ~PionSchedulerFactory()
 {
 }

 virtual PionSchedulerPtr Create() const = 0;
};

struct PionSingleServiceSchedulerFactory : public PionSchedulerFactory
{

 PionSingleServiceSchedulerFactory(int _threads = 8) 
  : PionSchedulerFactory(_threads)
 {
 }

 virtual PionSchedulerPtr Create() const
 {
  PionSingleServiceSchedulerPtr p(new pion::PionSingleServiceScheduler);
  p->setNumThreads(threads);
  return boost::dynamic_pointer_cast<pion::PionScheduler>(p);
 }

};

struct PionOneToOneSchedulerFactory : public PionSchedulerFactory
{

 PionOneToOneSchedulerFactory(int _threads = 8) 
  : PionSchedulerFactory(_threads)
 {
 }

 virtual PionSchedulerPtr Create() const
 {
  PionOneToOneSchedulerPtr p(new pion::PionOneToOneScheduler);
  p->setNumThreads(threads);
  return boost::dynamic_pointer_cast<pion::PionScheduler>(p);
 }

};

typedef boost::shared_ptr<PionSchedulerFactory> PionSchedulerFactoryPtr;

typedef boost::shared_ptr<PionSingleServiceSchedulerFactory> 
 PionSingleServiceSchedulerFactoryPtr;

typedef boost::shared_ptr<PionOneToOneSchedulerFactory> 
 PionOneToOneSchedulerFactoryPtr;

/**
WSGI web server that embeds the WSGI handler.
*/
class WsgiWebServer : public pion::net::WebServer
{
public:

 /**
 Constructor.

 @param endpoint TCP endpoint.
 @param resource WSGI resource string.
 @param handler WSGI handler.
 */
 WsgiWebServer(const boost::asio::ip::tcp::endpoint& endpoint,
               const std::string& resource, 
               boost::python::object& handler);

 /**
 Constructor.

 @param _scheduler Scheduler it used.
 @param endpoint TCP endpoint.
 @param resource WSGI resource string.
 @param handler WSGI handler.
 */
 WsgiWebServer(PionSchedulerPtr& _scheduler,
               const boost::asio::ip::tcp::endpoint& endpoint,
               const std::string& resource, 
               boost::python::object& handler);

 /**
 Destructor.
 */
 virtual ~WsgiWebServer();

/**
@name Overwrite methods.
*/
//@{

 void start(void);

 void stop(void);

 void loadService(const std::string& resource, const std::string& service);

 // We do not overwrite these methods but it will be needed when using Py++.
 /*

	void setServiceOption(const std::string& resource,
						  const std::string& name, const std::string& value);
	
	void loadServiceConfig(const std::string& config_name);
 */
//@}

public:

 /**
 Accessor to get handler.

 @return WSGI handler.
 */
 const boost::python::object& getHandler()
 {
  return handler;
 }

protected:

 /**
 Initialize.

 @param endpoint TCP endpoint.
 @param resource WSGI resource string.
 @param handler WSGI handler.
 */
 bool init(const boost::asio::ip::tcp::endpoint& endpoint,
           const std::string& resource, 
           boost::python::object& handler);

private:

 /// FIXME: Hold a shared ptr to scheduler will cause error in destructor.
 // It seems a bug of pion-net, it make it is impossible to use user-created 
 // scheduler.
 PionSchedulerPtr scheduler;

 /// WSGI handler.
 boost::python::object handler;

 /// WSGI cleaned?
 bool cleaned;

};

typedef boost::shared_ptr<WsgiWebServer> WsgiWebServerPtr;

/**
Public API to create web server instance.

@param handler WSGI handler.
@param resource Resource string that WSGI handler will handle.
@param port Port number.
@param scheduler Scheduler instance that will be used in server.
@param ip IP address.
@param log Show log for web server or not.
@return WsgiWebServer instance.
*/
WsgiWebServerPtr CreateWebServer(
 boost::python::object& handler,
 const std::string& resource = std::string("/"),
 const boost::uint32_t port = 8080, 
 //PionSchedulerFactoryPtr scheduler = PionSchedulerFactoryPtr(),
 const std::string& ip = std::string(), 
 bool log = true);

/**
Public API to create PionSingleServiceSchedulerFactory.
It is one io service for all threads in pool.

@param threads The number of threads in pool.
@return PionSingleServiceSchedulerFactory instance.
*/
PionSchedulerFactoryPtr CreatePionSingleServiceSchedulerFactory(
 const boost::uint32_t threads = 8);

/**
Public API to create PionOneToOneSchedulerFactory.
It is per io service for all threads in pool.

@param threads The number of threads in pool.
@return PionOneToOneSchedulerFactory instance.
*/
PionSchedulerFactoryPtr CreatePionOneToOneSchedulerFactory(
 const boost::uint32_t threads = 8);

#endif

#endif
